﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace proRFID_DiemDanh.DiemDanh
{
    public partial class DiemDanh : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(Request["MASV"]))
                {
                    giaoviencbo(0);
                    monhoccbo(0);
                    phongcbo(0);
                    lopcbo(0);
                    buoicbo(0);
                }
                else
                {
                    string ngayhoc = mdDiemDanh.ngayhoc;
                    string ngay = ngayhoc.Substring(0, 2);
                    string thang = ngayhoc.Substring(3, 2);
                    string nam = ngayhoc.Substring(6, 4);
                    string chuoingayhoc = nam + "-" + thang + "-" + ngay;
                    giaoviencbo(mdDiemDanh.magv);
                    txtNgayhoc.Text = chuoingayhoc;
                    monhoccbo(mdDiemDanh.monhoc);
                    phongcbo(mdDiemDanh.phong);
                    lopcbo(mdDiemDanh.lop);
                    buoicbo(mdDiemDanh.buoi);
                    doluoi();
                }
            }
           

        }
        void giaoviencbo(int magv)
        {
            ddlGiaoVien.DataSource = mdDiemDanh.DiemDanh_GiaoViencbo(magv);
            ddlGiaoVien.DataValueField = "MAGV";
            ddlGiaoVien.DataTextField = "HOTEN";
            ddlGiaoVien.DataBind();
        }
        void monhoccbo(int mamon)
        {
            ddlMon.DataSource =mdDiemDanh.DiemDanh_moncbo(mamon);
            ddlMon.DataValueField = "MAMH";
            ddlMon.DataTextField = "TENMH";
            ddlMon.DataBind();
        }
        void phongcbo(int maphong)
        {
            ddlPhong.DataSource = mdDiemDanh.DiemDanh_phongcbo(maphong);
            ddlPhong.DataValueField = "MAPHONG";
            ddlPhong.DataTextField = "TENPHONG";
            ddlPhong.DataBind();
        }
        void lopcbo(int malop)
        {
            ddlLop.DataSource = mdDiemDanh.DiemDanh_lopcbo(malop);
            ddlLop.DataValueField = "MALOP";
            ddlLop.DataTextField = "TENLOP";
            ddlLop.DataBind();
        }
        void buoicbo(int Buoi)
        {
            ddlBuoi.DataSource = mdDiemDanh.DiemDanh_Buoicbo(Buoi);
            ddlBuoi.DataValueField = "BUOIHOC";
            ddlBuoi.DataTextField = "buoi";
            ddlBuoi.DataBind();
        }
        void doluoi()
        {
            string ngayhoc = txtNgayhoc.Text;
            string ngay = ngayhoc.Substring(5, 2);
            string thang = ngayhoc.Substring(8, 2);
            string nam= ngayhoc.Substring(0, 4);

            string chuoingayhoc = ngay + "/" + thang + "/" + nam;
            int buoihoc = Convert.ToInt32(ddlBuoi.SelectedValue);
            int magv = Convert.ToInt32(ddlGiaoVien.SelectedValue);
            int mamh = Convert.ToInt32(ddlMon.SelectedValue);
            int maphong = Convert.ToInt32(ddlPhong.SelectedValue);
            int malop = Convert.ToInt32(ddlLop.SelectedValue);
            DataTable dt = mdDiemDanh.DiemDanh_Select(chuoingayhoc, buoihoc, magv, mamh, maphong, malop);
            if (dt.Rows.Count > 0)
            {
                rpt_danhsach.DataSource = dt;
                rpt_danhsach.DataBind();
                lbl_thongbao.Text = "";
            }
            else
                lbl_thongbao.Text = "Không có dữ liệu";
        }
        protected void cmd_xem_Click(object sender, EventArgs e)
        {
            if (txtNgayhoc.Text == "dd/mm/yyyy" || ddlBuoi.SelectedIndex == 0 || ddlGiaoVien.SelectedIndex == 0 || ddlLop.SelectedIndex == 0 || ddlMon.SelectedIndex == 0 || ddlPhong.SelectedIndex == 0)
            {
                lbl_thongbao.Text = "Chưa đủ thông tin";
                return;
            }
            doluoi();
        }
    }
}